<?php
    use User\Model\MemberServiceModel;
    use User\Model\MemberServiceItemModel;
?>
<div class="row">
    <div class="col-lg-12">
        <section class="panel">
            <header class="panel-heading">
                服务项目
            </header>
            <div class="panel-body">
                <form class="form-horizontal" method="post"
                      action="<?php if($type=='add'){echo '/User/Member/itemAdd';}
                      else{echo '/User/Member/itemUpdate';}?>" style="width: 70%;" >
                    <input type="hidden" value="<?php $val = (!empty($member_service_item)) ?
                            $member_service_item['item_id'] : ''; echo $val;?>" name="itemId">
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">名称:</label>

                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="name" value="<?php $val=(!empty($member_service_item)) ?
                            $member_service_item['name'] : ''; echo $val;?>">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">会员服务:</label>

                        <div class="col-sm-10" style="position: relative;">
                            <input type="text" class="form-control my_service" name="service" value="<?php $val=(!empty($service_name)) ?
                            $service_name : ''; echo $val;?>" id="service">
                            <input type="hidden" name="service_id" value="<?php $val=(!empty($member_service_item)) ?
                            $member_service_item['service_id'] : ''; echo $val;?>" id="service_id">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label">费用:</label>

                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="fee" value="<?php $val=(!empty($member_service_item)) ?
                            $member_service_item['fee'] : ''; echo $val;?>">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label ">项目开始时间</label>
                        <div class="col-sm-10">
                            <input class="form-control date-picker" name="valid_start_date" value="{$member_service_item['valid_start_date']|date='Y-m-d H:i:s', ###}" placeholder="请输入项目开始时间">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label ">项目结束时间</label>
                        <div class="col-sm-10">
                            <input class="form-control date-picker" name="valid_end_date" value="{$member_service_item['valid_end_date']|date='Y-m-d H:i:s', ###}" placeholder="请输入项目结束时间">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label">提醒:</label>
                        <div class="col-sm-10">
                        <?php
                            foreach(MemberServiceItemModel::getRemind() as $key=>$val_remind){
                                echo '<div class="radio"><input type="radio" name="is_remind" value="' . $key . '"';
                                if(!empty($member_service_item) && $member_service_item['is_remind']){
                                    echo " checked ";
                                }
                                echo '>' . $val_remind . '</div>';
                            }
                        ?>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label ">描述:</label>

                        <div class="col-sm-10">
                            <textarea type="text" class="form-control" name="describe" ><?php $val=(!empty($member_service_item)) ?
                            $member_service_item['describe'] : ''; echo $val;?></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-10">
                            <button class="btn btn-primary" type="submit" id="submit">提交</button>
                            <button class="btn btn-default" type="button" onclick="history.back(-1);">取消</button>
                        </div>
                    </div>

                </form>
            </div>
        </section>
    </div>
</div>
<script>
    $(".need").each(function(){
        $(this).prepend("<span style='color:red'>*</span>");
    });
    $("#submit").click(function(){
        var data = $("form").serialize();
        var url = $("form").attr("action");
        $.ajax({
            type:'get',
            dataType:'json',
            data:data,
            url:url,
            success:function(res){
                if(res.code == 1){
                    alert("提交成功");
                    window.location = '/User/Member/item';
                }else{
                    alert(res.msg);
                }
            }
        });
        return false;
    });

    function setService(name,code){
        $("#service").val(name);
        $("#service_id").val(code);
        $(".option").remove();
    }
    $('#service').bind('input', function () {
        $(".option").remove();
        $.ajax({
            data:{
                service:$("#service").val()
            },
            dataType:'json',
            url:"/User/Member/serviceSelect",
            success:function(res){
                var my_top = 35;
                for( var service in res ){
                    $("#service").after("<div class='option' style='position: absolute;top:" + my_top +
                            "px;z-index: 1000;border: 1px solid green;border-radius: 4px;background-color: white;font-size: larger;width: 500px;' " +
                            "onclick=" + '"' + "setService(" + "'" + res[service] + "'" + "," + "'" + service + "'"
                            + ")" + '"' + ">" + res[service] + "</div>");
                    my_top = my_top + 20;
                }
            }
        });
    });
    $("body").click(function(event){
        var target = $(event.target);
        if(!target.hasClass("my_service") && !target.hasClass("option")){
            $(".option").remove();
        }
    });
</script>
<script type="text/javascript" src="/Public/js/laydate/laydate.js"></script>
<script type="text/javascript">
    $( ".date-picker" ).bind( "click", function(){
        laydate( {
            format: 'YYYY/MM/DD hh:mm:ss',
            min: laydate.now(), //设定最小日期为当前日期
            max: '2099-06-16 23:59:59', //最大日期
            istime: true,
            istoday: false
        } );
        laydate.skin( "molv" );
    });
</script>